package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.sports.dto.LatelStuSportDto;
import com.wkbb.sports.model.HlifeStudentPractice;
import com.wkbb.sports.model.HlifeStudentPracticeProject;
import com.wkbb.sports.service.PracticeService;
import com.wkbb.sports.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lbh
 * @date 2021年03月29日 17:58:34
 * @description:   练习控制类
 */
@RestController
@Api(tags = "练习接口")
@RequestMapping("/sports/{apiVersion}/practice")
@Slf4j
public class PracticeController extends BaseController {

    @Autowired
    private PracticeService practiceService;

    @ApiOperation(value = "获取学生校外最近N天跳绳个人分析(默认30天)")
    @RequestMapping(value = "getLatelyStuSport", method = RequestMethod.GET)
    public ResultDTO<LatelStuSportDto> getLatelyStuSport(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学生id", required = true) @RequestParam(name = "studentId", required = true) String studentId,
            @ApiParam(value = "运动项目 1跳绳 ", required = false, defaultValue = "1") @RequestParam(name = "projectKind", required = false) Byte projectKind,
            @ApiParam(value = "最近天数,默认30天", required = false,defaultValue = "30") @RequestParam(name = "day", required = false) Integer day){
        UserDto userDto = this.getUserByLogin();
        if(null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        Map<String, Object> params = new HashMap<>();
        params.put("studentId", studentId);
        params.put("projectKind", projectKind);
        //1小时内 心率异常 实时参与人数
        if (day == null || day == 0 ) day = 30;
        params.put("beginDate", DateUtil.addCurrDay(-day));
        params.put("endDate", DateUtil.getDate());
        return practiceService.getLatelyStuSport(params);
    }

    @ApiOperation(value = "分页获取学生校外运动记录")
    @RequestMapping(value = "getSportListByStudent", method = RequestMethod.GET)
    public ResultDTO<PageInfo<HlifeStudentPractice>> getSportPracticePageList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学生id", required = true) @RequestParam(name = "studentId", required = true) String studentId,
            @ApiParam(value = "运动项目 1跳绳 ", required = false, defaultValue = "1") @RequestParam(name = "projectKind", required = false) Byte projectKind,
            @ApiParam(value = "分页数", defaultValue = "1") @RequestParam(name = "pageNo") int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(name = "pageSize") int pageSize){
        UserDto userDto = this.getUserByLogin();
        if(null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        Map<String,Object> map  = new HashMap<>();
        map.put("studentId", studentId);
        map.put("projectKind", projectKind);
        try {
            PageInfo<HlifeStudentPractice> page = practiceService.getListByPage(map,pageNo,pageSize);
            for (HlifeStudentPractice item:page.getList()){
                switch (item.getModelType()){
                    case 1:item.setModelTypeName("限时模式");break;
                    case 2:item.setModelTypeName("限数模式");break;
                    case 3:item.setModelTypeName("自由模式");break;
                }
            }
            return new ResultDTO<>(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取学生校外运动记录详情")
    @RequestMapping(value = "getSportPractice", method = RequestMethod.GET)
    public ResultDTO<HlifeStudentPractice> getSportPractice(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "编号", required = false) @RequestParam(name = "id", required = false) Long id){
        UserDto userDto = this.getUserByLogin();
        if(null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        try {
            return new ResultDTO<>(practiceService.getByPrimaryKey(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "某一个学生的练习列表或某一个练习详情")
    @RequestMapping(value = "getStudentPracticeListByStudentId", method = RequestMethod.GET)
    public ResultDTO<PageInfo<HlifeStudentPractice>> getStudentPracticeListByStudentId(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Integer orgId,
            @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) Integer studentId,
            @ApiParam(value = "练习ID", required = false) @RequestParam(name = "practiceId", required = false) Integer practiceId,
            @ApiParam(value = "分页数", defaultValue = "0", required = false) @RequestParam(name = "pageNo", defaultValue = "0", required = false) int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "20", required = false) @RequestParam(name = "pageSize", defaultValue = "20", required = false) int pageSize){
        return practiceService.getStudentPracticeListByStudentId(orgId,studentId,practiceId,pageNo,pageSize);
    }

    @ApiOperation(value = "获取项目时长")
    @RequestMapping(value = "getPracticeProject", method = RequestMethod.GET)
    public ResultDTO<HlifeStudentPracticeProject> getPracticeProject(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) String studentId){
        UserDto userDto = this.getRouteUserByUnion();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        return practiceService.getPracticeProject(studentId,userDto);
    }

}